package com.example.doubt.dialog.temp;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

public class LeakMemeryTest {




	public static void main(String[] args) {
		LeakMemeryTest t = new LeakMemeryTest();
		ReferenceQueue<LeakMemeryTest> queue = new ReferenceQueue<>();
		WeakReference<LeakMemeryTest> ref = new WeakReference<>(t, queue);
		printQueue(queue);
		t = null;
		System.out.println("\n释放变量.\n开始GC...\n");
		System.gc();
		printQueue(queue);
	}

	private static void printQueue(ReferenceQueue<LeakMemeryTest> queue){
		Reference<? extends LeakMemeryTest> temp;
		System.out.println("queue ↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
		do{
			temp = queue.poll();
			if(temp != null){
				System.out.println("");
			}
		}
		while (temp!=null);
		System.out.println("queue ↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
	}

}
